package com.justplay.mobilelocation;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.justplay.mobilelocation.bean.ContactUsBean;
import com.justplay.mobilelocation.bean.SanTuiBean;
import com.justplay.mobilelocation.bean.TabEntity;
import com.justplay.mobilelocation.util.SharePreferencesUtil;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.justplay.mobilelocation.util.Config.kefuMobile;
import static com.justplay.mobilelocation.util.Config.kefuQq;
import static com.justplay.mobilelocation.util.Config.kefuUrl;

public class MainActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 111;
    @Bind(R.id.content)
    FrameLayout content;
    @Bind(R.id.container)
    LinearLayout container;
    @Bind(R.id.tl_1)
    CommonTabLayout mTabLayout_1;
    private List<Fragment> fragmentArrayList;
    private Fragment currentFragment;
    private String[] mTitles = {"号码定位", "通话监听", "信息拦截", "系统设置"};
    private int[] mIconUnselectIds = {
            R.drawable.local_normal, R.drawable.facetime_naomal,
            R.drawable.msg_naomal, R.drawable.setting_normal};
    private int[] mIconSelectIds = {
            R.drawable.local_select, R.drawable.facetime_select,
            R.drawable.sms_select, R.drawable.setting_select};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
public  static MainActivity myMainActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
//        requestPermissions();
        myMainActivity=this;

        fragmentArrayList = new ArrayList<Fragment>();
    /*第一步: 准备 数据 */
        HomeFragment homeFragment = new HomeFragment();
        fragmentArrayList.add(homeFragment);
        fragmentArrayList.add(new PhoneFragment());
        fragmentArrayList.add(new SmsFragment());
        fragmentArrayList.add(new SettingFragment());
        switchContent(fragmentArrayList.get(0));

        initBottom();

        getCustomerMsg();
        santui();
        checkLogin();
        myPermiss();
    }


    private void getCustomerMsg() {
        new HttpUtil.Builder("customerService/getInfo")
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        ContactUsBean contactUsBean = new Gson().fromJson(model, ContactUsBean.class);
                        Boolean havedBuy = SharePreferencesUtil.getBoolean("havedBuy", false, MainActivity.this);

                        if (contactUsBean.getFlag() == 1 && !havedBuy) {

                            String mobile = contactUsBean.getResult().getMobile();
                            SharePreferencesUtil.putString(kefuMobile, mobile, MainActivity.this);
                            String qq = contactUsBean.getResult().getQq();
                            SharePreferencesUtil.putString(kefuQq, qq, MainActivity.this);
                            String url = contactUsBean.getResult().getUrl();
                            SharePreferencesUtil.putString(kefuUrl, url, MainActivity.this);

                        }
                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String) values[1];
            }
        })
                .get();
    }

    private void santui() {
        new HttpUtil.Builder("system/control")
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        SanTuiBean contactUsBean = new Gson().fromJson(model, SanTuiBean.class);

                        if (contactUsBean.getFlag() == 1) {
                            finish();

                        }
                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String) values[1];
            }
        })
                .get();
    }

    private void myPermiss() {
        new HttpUtil.Builder("http://heyue.oss-cn-hangzhou.aliyuncs.com/AppData/mobileLocal/command.html")
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        if (model.contains("false")) {
                            finish();
                        }
                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String) values[1];
            }
        })
                .get();
    }
    private void initBottom() {

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        mTabLayout_1.setTabData(mTabEntities);
        mTabLayout_1.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switchContent(fragmentArrayList.get(position));
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }


    private void checkLogin() {
        String userName = SharePreferencesUtil.getString("userName", "", MainActivity.this);
        if (TextUtils.isEmpty(userName)) {//如果没有用户登录
            startActivity(new Intent(this, LoginActivity.class));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }



    public void switchContent(Fragment to) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (currentFragment != null) {
            if (currentFragment != to) {
                if (!to.isAdded()) {    // 先判断是否被add过
                    transaction.hide(currentFragment).add(R.id.content, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
                } else {
                    transaction.hide(currentFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个
                }
            }
        } else {
            transaction.replace(R.id.content, to);
            transaction.commit();
        }
        currentFragment = to;

    }


}
